package core.devin.com.dmapplication;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.register.HuaWeiRegister;
import com.alibaba.sdk.android.push.register.MiPushRegister;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * Created by apple on 2017/6/22.
 */

public class CloudPushPlugin extends CordovaPlugin {

    public static NotificationEvent openNotificationEvent = null ;
    public static NotificationEvent receiveNotificationEvent = null ;

    private BadgeImpl badgeImpl = new BadgeImpl();

    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        this.initCloudChannel(cordova.getActivity().getApplication());
    }


    private static final String TAG = "Init";

    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        final CloudPushService pushService = PushServiceFactory.getCloudPushService();
        // 初始化小米通道，自动判断是否支持小米系统推送，如不支持会跳过注册
        MiPushRegister.register(applicationContext, "小米AppID", "小米AppKey");
        // 初始化华为通道，自动判断是否支持华为系统推送，如不支持会跳过注册
        HuaWeiRegister.register(applicationContext);
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.i(TAG, "init cloudchannel success");
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.e(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });

    }

//    public void initHawWei(Context applicationContext) {
//        PushManager.requestToken(applicationContext);
//    }

    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        boolean result = true ;
        if ("initCloudPush".equals(action)) {
            result = initCloudPush(args,callbackContext);
        } else if("registerAccount".equals(action)) {
            result = registerAccount(args,callbackContext);
        } else if("unRegisterAccount".equals(action)) {
            result = unRegisterAccount(args,callbackContext);
        } else if("registerTag".equals(action)) {
            result = registerTag(args,callbackContext);
        } else if("unRegisterTag".equals(action)) {
            result = unRegisterTag(args,callbackContext);
        } else if("listTags".equals(action)) {
            result = listTags(args,callbackContext);
        } else if("registerAlias".equals(action)) {
            result = registerAlias(args,callbackContext);
        } else if("unRegisterAlias".equals(action)) {
            result = unRegisterAlias(args,callbackContext);
        } else if("listAliases".equals(action)) {
            result = listAliases(args,callbackContext);
        } else if("setLocalNotification".equals(action)) {
            result = setLocalNotification(args,callbackContext);
        } else if("getUserNotificationSettings".equals(action)) {
            result = getUserNotificationSettings(args,callbackContext);
        } else if("clearAllLocalNotifications".equals(action)) {
            result = clearAllLocalNotifications(args,callbackContext);
        } else if("deleteLocalNotificationWithIdentifierKey".equals(action)) {
            result = deleteLocalNotificationWithIdentifierKey(args,callbackContext);
        } else if("getApplicationIconBadgeNumber".equals(action)) {
            result = getApplicationIconBadgeNumber(args,callbackContext);
        } else if("requestPermission".equals(action)) {
            result = requestPermission(args,callbackContext);
        } else if("receiveNotificationObservable".equals(action)) {
            result = receiveNotificationObservable(args,callbackContext);
        } else if("openNotificationObservable".equals(action)) {
            result = openNotificationObservable(args,callbackContext);
        } else {
            return false;
        }


        return result ;
    }

    public boolean initCloudPush(JSONArray args, CallbackContext callbackContext) {
        return true ;
    }

    public boolean registerAccount(JSONArray args, final CallbackContext callbackContext) {
        String account = "";
        if (args != null && args.length() > 0) {
            try {
                account = args.getString(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        PushServiceFactory.getCloudPushService().bindAccount(account, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }
            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }
            }
        });
        return true ;
    }

    public boolean unRegisterAccount(JSONArray args,final CallbackContext callbackContext) {
        PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }
            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }
            }
        });
        return true ;
    }

    public boolean registerTag(JSONArray args,final CallbackContext callbackContext) {
        String tag = "";
        if (args != null && args.length() > 0) {
            try {
                tag = args.getString(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        String[] tags = new String[]{
                tag
        };
        PushServiceFactory.getCloudPushService().bindTag(0,tags,null, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }
            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }
            }
        });
        return true ;
    }

    public boolean unRegisterTag(JSONArray args,final CallbackContext callbackContext) {
        String tag = "";
        if (args != null && args.length() > 0) {
            try {
                tag = args.getString(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        String[] tags = new String[]{
                tag
        };
        PushServiceFactory.getCloudPushService().unbindTag(0,tags,null, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }
            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }
            }
        });
        return true ;
    }

    public boolean listTags(JSONArray args,final CallbackContext callbackContext) {
        PushServiceFactory.getCloudPushService().listTags(0, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }
            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }            }
        });
        return true ;
    }

    public boolean registerAlias(JSONArray args,final CallbackContext callbackContext) {
        String alias = "";
        if (args != null && args.length() > 0) {
            try {
                alias = args.getString(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        PushServiceFactory.getCloudPushService().addAlias(alias, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }            }
        });
        return true ;
    }

    public boolean unRegisterAlias(JSONArray args,final CallbackContext callbackContext) {
        String alias = "";
        if (args != null && args.length() > 0) {
            try {
                alias = args.getString(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        PushServiceFactory.getCloudPushService().removeAlias(alias, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }            }
        });
        return true ;
    }

    public boolean listAliases(JSONArray args,final CallbackContext callbackContext) {
        PushServiceFactory.getCloudPushService().listAliases(new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                if (callbackContext != null) {
                    callbackContext.success(s);
                }            }

            @Override
            public void onFailed(String s, String s1) {
                if (callbackContext != null) {
                    callbackContext.error(s);
                }            }
        });
        return true ;
    }

    public boolean setLocalNotification(JSONArray args, CallbackContext callbackContext) {
        return true ;
    }

    public boolean deleteLocalNotificationWithIdentifierKey(JSONArray args, CallbackContext callbackContext) {
        return true ;
    }

    public boolean clearAllLocalNotifications(JSONArray args, CallbackContext callbackContext) {
        return true ;
    }

    public boolean getApplicationIconBadgeNumber(JSONArray args, CallbackContext callbackContext) {
        callbackContext.success(badgeImpl.getBadge(cordova.getActivity()));
        return true ;
    }

    public boolean setApplicationIconBadgeNumber(JSONArray args, CallbackContext callbackContext) {
        badgeImpl.setBadge(args,cordova.getActivity());
        return true ;
    }

    public boolean getUserNotificationSettings(JSONArray args, CallbackContext callbackContext) {
        return true ;
    }

    public boolean requestPermission(JSONArray args, CallbackContext callbackContext) {
        return true ;
    }

    public boolean receiveNotificationObservable(JSONArray args, CallbackContext callbackContext) {
        receiveNotificationEvent = new NotificationEvent(callbackContext);
        return true ;
    }

    public boolean openNotificationObservable(JSONArray args, CallbackContext callbackContext) {
        openNotificationEvent = new NotificationEvent(callbackContext);
        return true ;
    }

    public static class NotificationEvent {

        private CallbackContext callbackContext ;

        public NotificationEvent(CallbackContext callbackContext) {
            this.callbackContext = callbackContext ;
        }

        public void send(PluginResult pluginResult) {
            if (this.callbackContext != null && pluginResult != null ) {
                this.callbackContext.sendPluginResult(pluginResult);
            }
        }
    }
/**
 * - (void)initCloudPush:(CDVInvokedUrlCommand*)command;
 - (void)registerAccount:(CDVInvokedUrlCommand*)command;
 - (void)unRegisterAccount:(CDVInvokedUrlCommand*)command;

 - (void)registerTag:(CDVInvokedUrlCommand*)command;
 - (void)unRegisterTag:(CDVInvokedUrlCommand*)command;
 //查询标签 target默认1
 - (void)listTags:(CDVInvokedUrlCommand*)command;

 - (void)registerAlias:(CDVInvokedUrlCommand*)command;
 - (void)unRegisterAlias:(CDVInvokedUrlCommand*)command;
 - (void)listAliases:(CDVInvokedUrlCommand*)command;

 - (void)setLocalNotification:(CDVInvokedUrlCommand*)command;
 - (void)deleteLocalNotificationWithIdentifierKey:(CDVInvokedUrlCommand*)command;
 - (void)clearAllLocalNotifications:(CDVInvokedUrlCommand*)command;
 - (void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command;
 - (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command;
 - (void)getUserNotificationSettings:(CDVInvokedUrlCommand*)command;
 - (void)requestPermission:(CDVInvokedUrlCommand*)command;

 - (void)receiveNotificationObservable:(CDVInvokedUrlCommand*)command;
 - (void)openNotificationObservable:(CDVInvokedUrlCommand*)command;
 */
}
